public without  sharing class InstrumentProjectBonusController {
    
    public String dataJson {get ;private set;}
    public String backId {get;private set;}
    public String saveJson {get;  set;}
    public Boolean isSuccess {get; private set;}
    public Boolean isErr {get; private set;}
    public String msg {get;private set;}

    private  InstrumentProjectBonus__c ia ; 

    public InstrumentProjectBonusController() {

        backId = System.currentPageReference().getParameters().get('id');

    }


    public void init (){
    	try {
    	    

    	     ia = [select Id,Status__c, Manager__c ,ProjectManager__c ,DeputyManager__c  from InstrumentProjectBonus__c where Id =:backId ];

    	     if( UserInfo.getUserId() != ia.Manager__c  && UserInfo.getUserId() != ia.DeputyManager__c && UserInfo.getUserId() != ia.ProjectManager__c  ) new DmlException('没有权限!');
    	     if(ia.Status__c == '已批准') throw new DmlException(ia.Status__c +'不能确认!');

    	}
    	catch (Exception e) {
    		isErr = true;
    	    ApexPages.addMessage( new ApexPages.Message(ApexPages.Severity.ERROR ,e.getMessage()));
    	}
    	
    }
    public void getDetails(){

    	List<Object> tmp = new List<Object>();
    	for(InstrumentProjectBonusDetails__c ipbd :[select Id ,ApplicationInstrument__r.Name ,ApplicationInstrument__c ,InstrumentModel__c,DirectDistribution__c ,CustomerLevel__c ,Year__c,SalesCategory__c,TargetBonusStandard__c,	Quarter__c, StandardBonus__c,
    		Participants1__c,Participants1__r.Name  ,Participants1Rate__c ,Participants2__c ,Participants2__r.Name ,Participants2Rate__c,
    		 Participants4__c ,Participants4__r.Name ,Participants4Rate__c
    		from InstrumentProjectBonusDetails__c where InstrumentBonus__c =:backId  ]){
    		tmp.add(ipbd.getPopulatedFieldsAsMap());
    	}
    	dataJson = EncodingUtil.urlEncode(JSON.serialize(tmp), 'UTF-8');

    }

    private Id trans(String s ){
    	if(String.isBlank(s))return null;
    	return Id.valueOf(s);
    }

    public void save(){
    	try {
    	    List<Object> saveList =(List<Object> ) JSON.deserializeUntyped(EncodingUtil.urlDecode(saveJson, 'UTF-8'));

    	    if(!saveList.isEmpty()){
    	    	System.debug(saveList);
    	    	List<InstrumentProjectBonusDetails__c> sLL = new List<InstrumentProjectBonusDetails__c>();

    	    	for (Object o   : saveList) {

    	    		Map<String,Object> mo = (Map<String,Object>)o;
    	    		InstrumentProjectBonusDetails__c sL = new InstrumentProjectBonusDetails__c();

    	    		sL.Id = Id.valueOf(DataUtil.getStringValue(mo.get('Id')));

    	    		
    	    		sL.Participants1Rate__c = DataUtil.getDecimalValue(mo.get('Participants1Rate__c'));
    	    		sL.Participants2Rate__c = DataUtil.getDecimalValue(mo.get('Participants2Rate__c'));
    	    		sL.Participants4Rate__c = DataUtil.getDecimalValue(mo.get('Participants4Rate__c'));

    	    		sL.Participants1__c = trans(DataUtil.getStringValue(mo.get('Participants1__c')));
    	    		sL.Participants2__c = trans(DataUtil.getStringValue(mo.get('Participants2__c')));
    	    		sL.Participants4__c = trans(DataUtil.getStringValue(mo.get('Participants4__c')));
    	    		sLL.add(sL);


    	    	}
    	    	SavePoint sp =  Database.setSavepoint();
    	    	try {

    	    		update sLL;
    	    		ia.Confirmed__c = true;
    	    		update ia;

    	    		msg = '保存成功!';
    	    		isSuccess = true;
    	    	    
    	    	}catch (Exception e) {
    	    	   Database.rollback(sp);
    	    	   throw e ;

    	    	}
    	    	

    	    }
    	}
    	catch (Exception e) {
    	    msg = '保存失败!'+e.getMessage()+e.getStackTraceString();
    	    isSuccess = false;
    	}
    	
    }


}